<?php
class Gmapsprofile_AdminSettingsController extends Core_Controller_Action_Admin
{

  public function indexAction() 
  {
	$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('gmapsprofile_admin_main', array(), 'gmapsprofile_admin_main_settings');

	$settings = Engine_Api::_()->getApi('settings', 'core');
	$this->view->form = $form = new Gmapsprofile_Form_Admin_Settings();
	$metaData = Engine_Api::_()->getApi('core', 'fields')->getFieldsMeta('user');
	$optionsData = Engine_Api::_()->getApi('core', 'fields')->getFieldsOptions('user');
	$profile_type = $metaData->getRowMatching('type', 'profile_type');
	$LevelOption = $optionsData->getRowsMatching('field_id', $profile_type->field_id);
	foreach( $LevelOption as $option ) {
	  $level = 'gmapsprofile_level_' . $option->option_id;
      $block = $form->getElement('level_' . $option->option_id);
	  if (isset($block))
	    $form->getElement('level_' . $option->option_id)->setValue(isset($settings->$level) ? explode(';', $settings->$level) : '');
	}		
	
    $form->getElement('scale_profile')->setValue(isset($settings->gmapsprofile_scale_profile) ? $settings->gmapsprofile_scale_profile : 16);
    $form->getElement('type_map')->setValue(isset($settings->gmapsprofile_type_map) ? $settings->gmapsprofile_type_map : 'ROADMAP');
    $form->getElement('scale_friends')->setValue(isset($settings->gmapsprofile_scale_friends) ? $settings->gmapsprofile_scale_friends : 2);
    $form->getElement('type_map_friends')->setValue(isset($settings->gmapsprofile_tm_friends) ? $settings->gmapsprofile_tm_friends : 'ROADMAP');				
	
	if ( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {
	
	  foreach ( $this->getRequest()->getPost() as $sItem => $aValue ) {
		if ( strstr( $sItem, 'level_' ) ) {
		  $aField = $this->getRequest()->getPost( $sItem );
		  $iFieldID = (int)str_replace( 'level_', '', $sItem );
		  $level = 'gmapsprofile_level_' . $iFieldID;
		  $value = implode(';', $aField);
		  if (empty($value)) $value='';
		  $settings->$level = $value;
		}
	  }
	  $settings->gmapsprofile_scale_profile = $this->_getParam('scale_profile', 16);
	  $settings->gmapsprofile_type_map = $this->_getParam('type_map', 1);
	  $settings->gmapsprofile_scale_friends = $this->_getParam('scale_friends', 2);
	  $settings->gmapsprofile_tm_friends = $this->_getParam('type_map_friends', 1);
      $form->addNotice('Your changes have been saved.');
	}
  }

  public function levelAction()
  {
	$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('gmapsprofile_admin_main', array(), 'gmapsprofile_admin_main_level');
	  
		// Get level id
		if( null !== ($id = $this->_getParam('level_id')) ) 
			$level = Engine_Api::_()->getItem('authorization_level', $id);
		else
			$level = Engine_Api::_()->getItemTable('authorization_level')->getDefaultLevel();

		if( !$level instanceof Authorization_Model_Level )
			throw new Engine_Exception('missing level');
    
		$id = $level->level_id;

		// Make form
		$this->view->form = $form = new Gmapsprofile_Form_Admin_Level();
		$form->level_id->setValue($id);
		
		$permissionsTable = Engine_Api::_()->getDbtable('permissions', 'authorization');

		// Check post
		if( !$this->getRequest()->isPost() )
		{
			$form->populate($permissionsTable->getAllowed('gmapsprofile', $id, array_keys($form->getValues())));
			return;
		}

		// Check validitiy
		if( !$form->isValid($this->getRequest()->getPost()) )
			return;
		
		// Process
		$values = $form->getValues();
		$values = array('viewmy' => $values['viewmy'], 'viewfriend' => $values['viewfriend']);

		$db = $permissionsTable->getAdapter();
		$db->beginTransaction();

		try
		{
			// Set permissions
			$permissionsTable->setAllowed('gmapsprofile', $id, $values);

			// Commit
			$db->commit();
			
			$form->addNotice('Save ok');
		}

		catch( Exception $e )
		{
			$db->rollBack();
			throw $e;
		}
  
  }

  public function geocodeAction() 
  {
	$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('gmapsprofile_admin_main', array(), 'gmapsprofile_admin_main_geocode');

	$settings = Engine_Api::_()->getApi('settings', 'core');
	
	$form = $this->view->form = new Gmapsprofile_Form_Admin_Geocode();
	if ( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {
	  	$values = $this->getRequest()->getPost();
		if (isset($values['submit'])) {
	  		$settings->gmapsprofile_geocode = $this->_getParam('geocode', false);

			Engine_Api::_()->getDbtable('tasks', 'core')->delete(array(
      			'module = ?' => 'gmapsprofile',
    		));
	  
			if ($this->_getParam('geocode') == true) {
	    	  $insert_task = Engine_Api::_()->getDbtable('tasks', 'core');
		      $db = $insert_task->getAdapter();
		      $db->beginTransaction();

		      try
		      { 
	          $line = $insert_task->createRow();
	          $line->title = 'Gmap Geocode Update';
			  $line->module = 'gmapsprofile';
	          $line->plugin = 'Gmapsprofile_Plugin_Task_Geocode';
	          $line->timeout = $this->_getParam('timeout', 60);
			  $line->timeout = $this->_getParam('timeout', 60);
			  $line->save();
			
	          // Commit
	          $db->commit();
		      }
		      catch( Exception $e )
		      {
		        $db->rollBack();
		        throw $e;
		      }
	  		}	
		    $settings->gmapsprofile_queries = $this->_getParam('queries', 10);
		    $settings->gmapsprofile_timeout = $this->_getParam('timeout', 60);
			$form->addNotice('Settings saved');			
	  	  }
		  elseif (isset($values['clear_address'])) {
			Engine_Api::_()->getDbtable('markers', 'gmapsprofile')->delete();		  	
		  	$form->addNotice('Reset previous address');
		  }
	}
    $form->getElement('geocode')->setValue(isset($settings->gmapsprofile_geocode) ? $settings->gmapsprofile_geocode : false);
    $form->getElement('queries')->setValue(isset($settings->gmapsprofile_queries) ? $settings->gmapsprofile_queries : 10);	
	$form->getElement('timeout')->setValue(isset($settings->gmapsprofile_timeout) ? $settings->gmapsprofile_timeout : 60);	
  }
    
  //More Plugins
  public function moreAction()
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('gmapsprofile_admin_main', array(), 'gmapsprofile_admin_main_more');
  }
}